<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>流式响应</h1>
<div id="content"></div>
<form id="f1">
    <input id="prompt" type="text" name="prompt">
    <input type="button" value="提交">
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(function(){
         $(":button").on('click', function(){
             $("#content").empty();
             let prompt = $('#prompt').val();
             //创建事件源对象
             let eventSource = new EventSource('/ai/chat?prompt=' + prompt)
             //接收服务端的推送消息
             eventSource.onmessage = function (e) {
                 $("#content").append(e.data);
             }
             eventSource.onerror = function (e) {
                 eventSource.close();
             }
        })
    })
</script>
</body>
</html>